package flex.cc.uac.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import flex.cc.base.dto.LoginAuthDto;
import flex.cc.core.annotation.LogAnnotation;
import flex.cc.core.support.BaseController;
import flex.cc.uac.dto.role.RoleBindActionDto;
import flex.cc.uac.dto.role.RoleBindMenuDto;
import flex.cc.uac.dto.role.RoleBindUserDto;
import flex.cc.uac.entity.UacRoleEntity;
import flex.cc.uac.service.UacRoleService;
import flex.cc.uac.service.UacRoleUserService;
import flex.cc.uac.vo.BindAuthVo;
import flex.cc.uac.vo.RoleUserVo;
import flex.cc.wrapper.WrapMapper;
import flex.cc.wrapper.Wrapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 角色管理-公共方法.
 */
@RestController
@RequestMapping(value = "/uac/role")
public class UacRoleCommonController extends BaseController {

	@Resource
	private UacRoleService uacRoleService;
	@Resource
	private UacRoleUserService uacRoleUserService;

	/**
	 * 查看角色信息.
	 * @param id
	 * @return
	 */
	@PostMapping(value = "/queryRoleInfoById/{id}")
	@ApiOperation(httpMethod = "POST", value = "查看角色信息")
	public Wrapper<UacRoleEntity> queryRoleInfo(@PathVariable Long id) {
		UacRoleEntity role = uacRoleService.getById(id);
		return WrapMapper.ok(role);
	}

	/**
	 * 验证角色编码是否存在.
	 * @param checkRoleCodeDto
	 * @return
	 */
//	@PostMapping(value = "/checkRoleCode")
//	@ApiOperation(httpMethod = "POST", value = "验证角色编码是否存在")
//	public Wrapper<Boolean> checkUacRoleCode(@ApiParam(name = "roleCode", value = "角色编码") @RequestBody CheckRoleCodeDto checkRoleCodeDto) {
//
//		logger.info("校验角色编码唯一性 checkRoleCodeDto={}", checkRoleCodeDto);
//
//		Long id = checkRoleCodeDto.getRoleId();
//		String roleCode = checkRoleCodeDto.getRoleCode();
//
//		Example example = new Example(UacRole.class);
//		Example.Criteria criteria = example.createCriteria();
//
//		if (id != null) {
//			criteria.andNotEqualTo("id", id);
//		}
//		criteria.andEqualTo("roleCode", roleCode);
//
//		int result = uacRoleService.selectCountByExample(example);
//		return WrapMapper.ok(result < 1);
//	}

	/**
	 * 获取菜单树
	 * @param roleId  角色id
	 * @Param isEdit 是否编辑
	 */
	@GetMapping(value = "/getMenuTreeByRoleId/{roleId}")
	@ApiOperation(httpMethod = "GET", value = "获取菜单树")
	public Wrapper<BindAuthVo> getMenuTreeByRoleId(@ApiParam(name = "roleId", value = "角色id") @PathVariable Long roleId,String isEdit) {
		BindAuthVo bindAuthVo = uacRoleService.getMenuTreeByRoleId(roleId,isEdit);
		return WrapMapper.ok(bindAuthVo);
	}

	/**
	 * 获取权限树
	 * @param roleId  角色id
	 * @Param isEdit 是否编辑
	 * @return
	 */
	@GetMapping(value = "/getActionTreeByRoleId/{roleId}")
	@ApiOperation(httpMethod = "GET", value = "获取权限树")
	public Wrapper<BindAuthVo> getActionTreeByRoleId(
			@ApiParam(name = "roleId", value = "角色id") @PathVariable Long roleId,String isEdit) {
		BindAuthVo bindAuthVo = uacRoleService.getActionTreeByRoleId(roleId,isEdit);
		return WrapMapper.ok(bindAuthVo);
	}

	/**
	 * 获取角色绑定用户页面数据.
	 * @return
	 */
	@GetMapping(value = "/getBindUser/{roleId}")
	@ApiOperation(httpMethod = "GET", value = "获取角色绑定用户页面数据")
	public Wrapper<IPage<RoleUserVo>> getBindUser(
			@ApiParam(name = "roleId", value = "角色id") @PathVariable Long roleId,
			long pageNum,long pageSize,
			@RequestParam(required = false) String bindStatus,
			@RequestParam(required = false) String condition) {
		logger.info("获取角色绑定用户页面数据. roleId={}", roleId);
		IPage<RoleUserVo> roleUserVos = uacRoleUserService.getRoleUserWithPage(pageNum,pageSize,
				roleId,bindStatus,condition);
		return WrapMapper.ok(roleUserVos);
	}

	/**
	 * 绑定/解绑角色-用户.
	 */
	@LogAnnotation
	@PostMapping(value = "/bindUser")
	@ApiOperation(httpMethod = "POST", value = "绑定/解绑角色-用户")
	public Wrapper bindUser(@ApiParam(name = "uacRoleBindUserReqDto", value = "绑定/解绑角色-用户")
								@RequestBody RoleBindUserDto roleBindUserDto) {
		logger.info("roleBindUser={}", roleBindUserDto);
		LoginAuthDto loginAuthDto = getLoginAuthDto();
		uacRoleService.bindUserRole(roleBindUserDto, loginAuthDto);
		return WrapMapper.ok();
	}

	/**
	 * 分配菜单
	 *
	 */
	@PostMapping(value = "/bindMenu")
	@ApiOperation(httpMethod = "POST", value = "角色分配权限")
	@LogAnnotation
	public Wrapper bindMenu(@ApiParam(name = "bindMenu", value = "角色分配权限") @RequestBody RoleBindMenuDto roleBindMenuDto) {
		logger.info("角色分配权限. roleBindMenuDto= {}", roleBindMenuDto);
		uacRoleService.bindMenu(roleBindMenuDto);
		return WrapMapper.ok();
	}

	/**
	 * 分配权限.
	 */
	@PostMapping(value = "/bindAction")
	@ApiOperation(httpMethod = "POST", value = "角色分配权限")
	@LogAnnotation
	public Wrapper bindAction(@ApiParam(name = "bindAuth", value = "权限信息") @RequestBody RoleBindActionDto roleBindActionDto) {
		logger.info("角色分配权限. roleBindActionDto= {}", roleBindActionDto);
		uacRoleService.bindAction(roleBindActionDto);
		return WrapMapper.ok();
	}


}
